# -*- coding: utf-8 -*-
"""
Created on Thu Aug 23 11:59:02 2018

@author: Who

区块链见闻
http://www.qkljw.com/

"""

import requests
from cs import day
from bs4 import BeautifulSoup
from WriteData import writedata
from Tk import genearteMD5


def post_html_text(url, data):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
    }
    try:
        r = requests.post(url, timeout=30, data=data, headers=headers)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except Exception as e:
        print(e)
        return None


def get_html_text(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
    }
    try:
        r = requests.get(url, timeout=30, headers=headers)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except Exception as e:
        print(e)
        return None


def get_dates_kx():
    dates = []
    a = 0
    id = 999999999999
    while a < 40:
        data = {
            'n_id': id,
            'type': '1'
        }
        url = 'http://www.qkljw.com/News/getNews'
        print('----区块链见闻快讯第{}页获取中----'.format(a))
        a += 1
        text = post_html_text(url, data)
        if not text:
            continue
        soup = BeautifulSoup(text, 'lxml')
        data = soup.select('.time')
        ids = soup.select('.control')
        for id in ids:
            id = id.get('data')
        dates += [date.get_text() for date in data]

    return dates


def get_dates_zx(url):
    text = get_html_text(url)
    if not text:
        return None
    soup = BeautifulSoup(text, 'lxml')
    data = soup.select('.article_cate_li_info_date')
    dates = [date.get_text()[:10] for date in data]
    return dates


def count_process_one():
    dates = get_dates_kx()
    a = 0
    l = []
    try:
        while 1:
            if dates[a] < dates[a + 1]:
                l.append(a + 1)
            if len(l) == 3:
                break
            a += 1

        return (len(dates[:l[0]]), len(dates[l[0]:l[1]]), len(dates[l[1]:l[2]]))
    except:
        return (0, 0, 0)


def count_process_two():
    d1 = 0
    d2 = 0
    d3 = 0
    d4 = 0
    a = 1
    while 1:
        url = 'http://www.qkljw.com/Article/hot/p/{}.html'
        url = url.format(a)
        dates = get_dates_zx(url)
        if not dates:
            break
        for date in dates:
            s = day(date)
            if s == 0:
                d1 += 1
            elif s == 1:
                d2 += 1
            elif s == 2:
                d3 += 1
            else:
                d4 += 1
        print('----区块链见闻资讯第{}页获取中----'.format(a))
        a += 1
        if d4 > 0:
            break
    return (d1, d2, d3)


def main():
    u = '区块链见闻'
    t1 = count_process_one()
    t2 = count_process_two()
    tk = genearteMD5(u)
    D = {tk: {"name": u, "today": t1[0] + t2[0], "yesterday": t1[1] + t2[1], "frontday": t1[2] + t2[2]}}
    writedata(D)
    return D


if __name__ == '__main__':
    print(main())
